﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace CarRacing2
{
    class LightTraffic
    {
        private  int lightTrafficCounter2 = 0;
        Timer timer;

        public LightTraffic()
        {
            timer = new Timer(TimerCounter, null, 0, 1000); //таймер срабатывает каждую секкунду

            
        }
        public void TimerCounter(object obj)
        {
            lightTrafficCounter2++;
            if (lightTrafficCounter2 == 1)
            {
                if (LightTrafficEventGreen != null)
                {
                    LightTrafficEventGreen();
                }


            }
            if (lightTrafficCounter2 == 5) //по истечении 5-ти секунд врубаем желтый свет
            {
                if (LightTrafficEventYellow != null)
                {
                    LightTrafficEventYellow();
                }
                
            }
            if (lightTrafficCounter2 == 7)//по истечении 7-ми секунд врубаем красный свет
            {
                if (LightTrafficEventRed != null)
                {
                    LightTrafficEventRed();
                    lightTrafficCounter2 = 0;
                }

            }
            
        }
        public delegate void LightTrafficDelegate();
        
        public event LightTrafficDelegate LightTrafficEventGreen;
        public event LightTrafficDelegate LightTrafficEventYellow;
        public event LightTrafficDelegate LightTrafficEventRed;
       
    }
    class Car
    {
        LightTraffic tr = new LightTraffic();
        private  int stringNumber=0;
        private static int counter = 0;
        private  int _lightTrafficCounter;
        public  int  lightTrafficCounter 
        { 
            get
            {
                return _lightTrafficCounter;
            }
            set
            {
                _lightTrafficCounter = value;
            }
        }
        Timer timer;
        public Car(int strNumber)
        {
            stringNumber = strNumber;
            timer = new Timer(CarDrawing, null, Timeout.Infinite, Timeout.Infinite);
        }
        private  void CarDrawing(object obj)
        {
            Console.Clear();
            counter++;
            
            lightTrafficCounter++;


            if (counter == 80)
            {
                counter = 0;
            }
            int i=stringNumber;
            while(i>0)
            {
                Console.WriteLine();
                i--;
            }
            Console.Write(new String(' ', counter));
            Console.Write("car"+Convert.ToInt32(stringNumber+1));
        }

        public void Go()
        {
            timer.Change(0, 50);
            
            
        }

        public void SlowMotion()
        {
            timer.Change(0, 500);
        }
        public void Stop()
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            LightTraffic trLight = new LightTraffic();
          
            Car car = new Car(0);
            car.Go();
            trLight.LightTrafficEventGreen += car.Go;
            trLight.LightTrafficEventYellow += car.SlowMotion;
            trLight.LightTrafficEventRed += car.Stop;
          
            Car car2 = new Car(1);
            car2.Go();
            trLight.LightTrafficEventGreen += car2.Go;
            trLight.LightTrafficEventYellow += car2.SlowMotion;
            trLight.LightTrafficEventRed += car2.Stop;

            Car car3 = new Car(2);
            car3.Go();
            trLight.LightTrafficEventGreen += car3.Go;
            trLight.LightTrafficEventYellow += car3.SlowMotion;
            trLight.LightTrafficEventRed += car3.Stop;
            object someObj = null;
            //Запускаем счетчик светофора
            trLight.TimerCounter(someObj);
            Console.ReadLine();
        }
    }
}
